﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StepPrice;

namespace StepPriceDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int gasAll = 0, jieZhuanGas = 0, renDingGas = 0, renDingShengYuGas = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            InitControl();
        }

        private void InitControl()
        {
            txtLimit1.Text = "200";
            txtLimit2.Text = "500";
            txtPrice1.Text = "1.63";
            txtPrice2.Text = "1.95";
            txtPrice3.Text = "2.45";
            txtGasAll.Text = "50";
            txtOpenDate.Value = DateTime.Parse("2017-05-01");
            dPickStartDate.Value = DateTime.Parse("2018-01-01");
            dPickEndDate.Value = DateTime.Parse("2018-12-31");
            rBtnGas.Checked = true;
        }
        /// <summary>
        /// 验证输入是否正确 
        /// </summary>
        /// <returns></returns>
        private bool CheckInput()
        {
            //阶梯内总购

            if (!int.TryParse(txtJieZhuang.Text.Trim(), out jieZhuanGas))
            {
                jieZhuanGas = 0;
            }
            if (!int.TryParse(txtGasAll.Text.Trim(), out gasAll))
            {
                gasAll = 0;
            }

            if (jieZhuanGas > gasAll)
            {
                txtJieZhuang.Focus();
                MessageBox.Show("结转气量填写不正确");
                return false;
            }

            //认定气量相关

            if (!int.TryParse(txtRenDing.Text.Trim(), out renDingGas))
            {
                renDingGas = 0;
            }

            if (!int.TryParse(txtRenDingShengYu.Text.Trim(), out renDingShengYuGas))
            {
                renDingShengYuGas = 0;
            }
            if (renDingShengYuGas > renDingGas)
            {
                txtRenDingShengYu.Focus();
                MessageBox.Show("认定剩余量填写不正确");
                return false;
            }
            if (renDingGas > 0)//认定气量大于0，各阶累计量必填
            {
                if (txtLeiJi1.Text.Trim().Length < 1)
                {
                    txtLeiJi1.Focus();
                    MessageBox.Show("一阶累计量未填写");
                    return false;
                }
                if (txtLeiJi2.Text.Trim().Length < 1)
                {
                    txtLeiJi2.Focus();
                    MessageBox.Show("二阶累计量未填写");
                    return false;
                }
                if (txtLeiJi3.Text.Trim().Length < 1)
                {
                    txtLeiJi3.Focus();
                    MessageBox.Show("三阶累计量未填写");
                    return false;
                }


                if (jieZhuanGas < 1)
                {
                    if (gasAll != (int.Parse(txtLeiJi1.Text)) + int.Parse(txtLeiJi2.Text) + int.Parse(txtLeiJi3.Text))
                    {
                        MessageBox.Show("无结转气量时，每阶气量相加必须等于阶梯内气量");
                        return false;

                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 按充值气量计算阶梯
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPayGas_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                    int payGas = 0;
                    if (int.TryParse(txtPayGas.Text, out payGas))
                    {
                        var info = new StepPriceInfo()
                        {
                            FlagGasAver = rBtnYear.Checked,//兰星为年均用量
                            FlagPayWay = false,//按购买量计算
                            LimitGas1 = int.Parse(txtLimit1.Text),
                            LimitGas2 = int.Parse(txtLimit2.Text),
                            Price1 = decimal.Parse(txtPrice1.Text),
                            Price2 = decimal.Parse(txtPrice2.Text),
                            Price3 = decimal.Parse(txtPrice3.Text),
                            StepStartDate = dPickStartDate.Value,
                            StepEndDate = dPickEndDate.Value,
                            OpenGasDate = txtOpenDate.Value.ToString("yyyyMMdd"),
                            GasAll = gasAll,
                            PayGas = payGas,
                            PayMoney = 0,
                            AccountBalance = decimal.Parse(txtOldYuE.Text),
                            RenDingGas = renDingGas,
                            RenDingShengYuGas = renDingShengYuGas,
                            JieZhuanGas = jieZhuanGas
                        };
                        if (renDingGas > 0)
                        {
                            info.LeiJiGas1 = int.Parse(txtLeiJi1.Text);
                            info.LeiJiGas2 = int.Parse(txtLeiJi2.Text);
                            info.LeiJiGas3 = int.Parse(txtLeiJi3.Text);
                        }

                        var stepPrice = new CalcStepPrice(info);
                        ShowStepPrice(stepPrice.CalcStep(), false);
                    }
                    else
                    {
                        ClearCntrValue(panel1);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



        /// <summary>
        /// 按充值金额计算阶梯
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPayMoney_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {

                    decimal payMoney = 0;
                    if (decimal.TryParse(txtPayMoney.Text, out payMoney))
                    {
                        var info = new StepPriceInfo()
                        {
                            FlagGasAver = rBtnYear.Checked,//兰星为年均用量
                            FlagPayWay = true,//按金额计算
                            LimitGas1 = int.Parse(txtLimit1.Text),
                            LimitGas2 = int.Parse(txtLimit2.Text),
                            Price1 = decimal.Parse(txtPrice1.Text),
                            Price2 = decimal.Parse(txtPrice2.Text),
                            Price3 = decimal.Parse(txtPrice3.Text),
                            StepStartDate = dPickStartDate.Value,
                            StepEndDate = dPickEndDate.Value,
                            OpenGasDate = txtOpenDate.Value.ToString("yyyyMMdd"),
                            GasAll = decimal.Parse(txtGasAll.Text),
                            PayGas = 0,
                            PayMoney = payMoney,
                            AccountBalance = decimal.Parse(txtOldYuE.Text),
                            RenDingGas = renDingGas,
                            RenDingShengYuGas = renDingShengYuGas,
                            JieZhuanGas = jieZhuanGas
                        };
                        if (renDingGas > 0)
                        {
                            info.LeiJiGas1 = int.Parse(txtLeiJi1.Text);
                            info.LeiJiGas2 = int.Parse(txtLeiJi2.Text);
                            info.LeiJiGas3 = int.Parse(txtLeiJi3.Text);
                        }
                        var stepPrice = new CalcStepPrice(info);
                        ShowStepPrice(stepPrice.CalcStep(), true);
                    }
                    else
                    {
                        ClearCntrValue(panel1);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        /// <summary>
        ///  显示阶梯计算结果
        /// </summary>
        /// <param name="info"></param>
        /// <param name="flag">false:气量，true:金额</param>
        private void ShowStepPrice(StepPriceInfo info, bool flag)
        {
            if (info == null)
            {
                MessageBox.Show("日期不正确");
                return;
            }
            if (!flag)
            {
                txtGas1.Text = info.Gas1.ToString();
                txtGas2.Text = info.Gas2.ToString();
                txtGas3.Text = info.Gas3.ToString();
                txtMoney1.Text = info.Money1.ToString();
                txtMoney2.Text = info.Money2.ToString();
                txtMoney3.Text = info.Money3.ToString();
                lblMoney.Text = info.PayMoney + "元";
            }
            else
            {
                txtPayGas_M.Text = info.PayGas.ToString();
                txtGas1_M.Text = info.Gas1.ToString();
                txtGas2_M.Text = info.Gas2.ToString();
                txtGas3_M.Text = info.Gas3.ToString();
                txtNewYuE.Text = info.AccountBalance.ToString();

            }
        }

        private void rBtnGas_CheckedChanged(object sender, EventArgs e)
        {
            txtPayGas.Enabled = true;
            txtPayMoney.Enabled = false;

            lblMoney.Text = "--元";
            ClearCntrValue(panel1);


        }

        private void rBtnMoney_CheckedChanged(object sender, EventArgs e)
        {
            txtPayGas.Enabled = false;
            txtPayMoney.Enabled = true;

            lblMoney.Text = "--元";
            ClearCntrValue(panel1);
        }



        /// <summary>
        /// 清除容器里面某些控件的值
        /// </summary>
        /// <param name="parContainer">容器类控件</param>
        public void ClearCntrValue(Control parContainer)
        {
            for (int index = 0; index < parContainer.Controls.Count; index++)
            {
                // 如果是容器类控件，递归调用自己
                if (parContainer.Controls[index].HasChildren)
                {
                    ClearCntrValue(parContainer.Controls[index]);
                }
                else
                {
                    switch (parContainer.Controls[index].GetType().Name)
                    {
                        case "TextBox":
                            parContainer.Controls[index].Text = "";
                            break;

                    }
                }
            }
        }

        private void rBtnYear_CheckedChanged(object sender, EventArgs e)
        {
            lblMoney.Text = "--元";
            ClearCntrValue(panel1);
        }





        private void lbl_Paint(object sender, PaintEventArgs e)
        {

            DrawBorder(e.Graphics, Color.SpringGreen, lblJieTiInfo);

        }
        /// <summary>
        /// 标签样式
        /// </summary>
        /// <param name="g"></param>
        /// <param name="bordercolor">边框颜色</param>
        /// <param name="lbl">label</param>
        private void DrawBorder(System.Drawing.Graphics g, Color bordercolor, Label lbl)
        {
            lbl.BorderStyle = BorderStyle.None;
            Rectangle myRectangle = new Rectangle(0, 0, lbl.Width, lbl.Height);
            ControlPaint.DrawBorder(g, myRectangle, bordercolor, ButtonBorderStyle.Solid);//画个边框



        }

    }
}
